package com.yundasys.lab02springbootstudy;

import com.yundasys.lab02springbootstudy.importselector.annotation.EnableSpringStudy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.AutoConfigurationPackages;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Arrays;
import java.util.List;

@SpringBootApplication
@EnableSpringStudy
public class Lab02SpringbootStudyApplication {

    private static final Logger logger = LoggerFactory
            .getLogger(Lab02SpringbootStudyApplication.class);


    public static void main(String[] args) {
        //SpringApplication.run(Lab02SpringbootStudyApplication.class, args);

        // System.setProperty("app.name", "dgm");
        // System.setProperty("app.desc", "dongguangming github");

        // System.setProperty("logType", "FILE");
//        String bdrack = System.getenv("BDRACK");
//        logger.error("配置文件："
//                + (bdrack != null ? ((bdrack.endsWith("/") ? bdrack
//                + "conf/config.properties" : bdrack
//                + "/conf/config.properties"))
//                : "conf/config.properties"));
//        System.setProperty("spring.config.location",
//                bdrack != null ? ((bdrack.endsWith("/") ? bdrack
//                        + "conf/config.properties" : bdrack
//                        + "/conf/config.properties"))
//                        : "conf/config.properties");
//        logger.error("获取配置文件：" + System.getProperty("spring.config.location"));

        // SpringApplication applicationContext = new SpringApplication(
        // SpringBootCustomStarterApplication.class);

        // app.setBannerMode(Banner.Mode.OFF);
        // applicationContext.setLogStartupInfo(false);
        // ConfigurableApplicationContext c = applicationContext.run(args);
        ConfigurableApplicationContext applicationContext = SpringApplication.run(
                Lab02SpringbootStudyApplication.class, args);

        List<String> packages = AutoConfigurationPackages
                .get(applicationContext);
        System.err.println("要扫描的包是packages: " + packages);

        String[] beanNames = applicationContext.getBeanDefinitionNames();
        List<String> beans = Arrays.asList(beanNames);
        System.err.println(beans);
        beans.forEach(bean -> {
            if (bean.contains("UserDAO") || bean.contains("cacheManager")
                    || bean.contains("LogService")) {

                System.out.println(bean);

            }
        });

        System.err.println("来自环境变量:");
        Environment env = (Environment) applicationContext.getEnvironment();
        System.out.println("- connection Name1111: " + env.getProperty("app.name"));
        System.out.println("- connection Url111: " + env.getProperty("app.url"));

//        ApplicationProperties appProperties = (ApplicationProperties) applicationContext
//                .getBean(ApplicationProperties.class);
//
//        System.err.println("Properties Values from Properties File:");
//        System.out.println("- connection Name: " + appProperties.getName());
//        System.out.println("- connection Url: " + appProperties.getUrl());

        // 我使用的最新版Springboot2.3.0(放弃1版本)启动后，不放心的话可以测试数据库是否连接正常
		/*UserMapper userMapper = applicationContext.getBean(UserMapper.class);
		Example example = new Example(User.class);
		example.createCriteria().andEqualTo("username", "dongguangming");
		List<User> userList = userMapper.selectByExample(example);
		if (userList.size() > 0) {
			User user = userList.get(0);
			System.err.println("数据库连接正常，从用户表取用户名是donggguangming的数据,用户：" + user);
		}*/

        // JdbcUserDAO jdbcUserDAO= (JdbcUserDAO) c.getBean("jdbcUserDAO");
        // System.out.println(jdbcUserDAO.getAllUserNames());
        // SpringApplication.run(SpringBootTestApplication.class, args);

        System.out.println("beanDemo:" + applicationContext.getBean("beanDemo"));

    }


//    @Bean
//    public DataLoader dataLoader() {
//        return new DataLoader();
//    }
//
//    @Bean
//    public AsyncService asyncService() { return new AsyncService();}
//    @Slf4j
//    class DataLoader implements CommandLineRunner {
//        @Override
//        public void run(String... strings) throws Exception {
//            log.info("Loading data...");
//
//            List<String> strList = new ArrayList<>();
//            CompletableFuture[] completableFutures = new CompletableFuture[strList.size()];
//            strList.forEach(str -> {
//                CompletableFuture<String> future = asyncService().doSomeThingAsync(str);
//                completableFutures[strList.indexOf(str)] = future;
//            });
//        }
//    }
}
